/**
 * 权限设置窗口
 * SelectRightWin.show(record.roleId,function(){
		grid.load();
	});
 */
var SelectRightWin = (function(){
	
	var listTopMenuUrl = ctx + 'listTopMenu.do'
	var listMenuUrl = ctx + 'listMenuByTabId.do';
	var listRolePermissionByRoleIdUrl = ctx + 'listRolePermissionByRoleId.do';
	var addRolePermissionUrl = ctx + 'addRolePermission.do';
	var delRolePermissionUrl = ctx + 'delRolePermission.do';
	
	var inited = false;
	var tab = null;
	var win = null;
	var tree = null;
	var treeStore;
	var grid = null;
	var gridStore;
	
	var curRoleId,curCallback;
	var curSfIds;
	
	var html = [
		'<div id="rightSelectDialog">'
			,'<div id="srw_tab"></div>'
			,'<div id="srw_tabCont">'
				,'<table>' 
					,'<tr>' 
						,'<td id="srw_treeMenu" valign="top"></td>' 
						,'<td id="srw_gridOpt" valign="top" style="padding-left:20px;"></td>' 
					,'<tr>' 
				,'</table>' 
			,'</div>'
		,'</div>'
	];
	
	function init(roleId,callback) {
		curCallback = callback;
		
		var sfIds = getData(roleId);
		
		setValue(roleId,sfIds);
		
		if(!inited) {
			inited = true;
			
			initPanel();
		}
	}
	
	function getData(roleId) {
		var sfIds = [];
		Action.postSync(listRolePermissionByRoleIdUrl,{roleId:roleId},function(sysFuns) {
			for(var i=0,len=sysFuns.length;i<len;i++){
				sfIds.push(sysFuns[i].sfId);	
			}
		});
		return sfIds;
	}
	
	function initPanel() {
		var panel = html.join('');
		$('body').append(panel);
		
		initTab();
		
		initTree();
		
		initGrid();
		
		initDialog();
	}
	
	function initTab() {
		
		Action.post(listTopMenuUrl,{},function(e){
			var rows = e;
			tab = new VUI.Tab({
				renderId:'srw_tab'
				,items:rows
				,border:false
				,loadFilter:function(items) {
					for(var i=0, len=items.length;i<len; i++) {
						items[i].title = items[i].tabName;
					}
				}
			});
			
			tab.on('Select',function(e){
				var item = e.item;
				tree.load({tabId:item.id});
			});
			
			tab.setNextDo(function(){
				this.select(0);
			})
			
		})
	}
	
	function initDialog() {
		
		win = new VUI.Dialog({
			contentId:'rightSelectDialog'
			,title:'设置权限'
			,width:650
			,height:490
			,buttons:[
			 { text:'关闭', handler : function(){ 
			 	this.close(); 
			 }}
			]
		});
		
		win.on('AfterClose',function(){
			curCallback();
		});
	}
	
	function initTree() {
		
		tree = new VUI.Tree({
			renderId:'srw_treeMenu'
			,width:250
	       	,height: 350
			,url:listMenuUrl
			,initLoad:false
			,clickToggle:true
			,renderer:function(item) {
				var ret = item.resName;
				var count = getOperateCount(item);
	        	var span = count > 0 
		        	? '<strong style="color:red;margin-left:5px;">'+count+'</strong>'
		        	: '';
		        	
	        	return ret + span;
			}
		});
		
		tree.on('BeforeLoad',function(e) {
	    	grid.removeData();
		});
		
		tree.on('Select',function(e) {
			var node = e.item;
	    	grid.loadData(node.sysFuns);
		});
	}
	
	function initGrid() {
		
		grid = new VUI.Grid({
		    renderId: 'srw_gridOpt',
		    initLoad:false,
		    striped:true, // 斑马线
		    columns: [[
		    	{field:'sfId',checkbox : true} // 勾选
				,{title : '权限功能',field :'operateName'}
			]],
		    width: 250,
		    height: 320
		});
        
        grid.on('Check',function(e){
        	var item = e.row;
        	if(item){
        		addRolePermission(item.sfId);
        	}
        });
        grid.on('Uncheck',function(e){
        	var item = e.row;
        	if(item){
        		delRolePermission(item.sfId);
        	}
        });
        
        grid.on('RenderSuccess',function(){
        	for(var j=0; j<curSfIds.length; j++) {
				grid.selectRecord(curSfIds[j]);
			}
        });
	}
	
	function getOperateCount(item) {
		var count = 0;
    	var sysFuns = item.sysFuns || [];
    	for(var i=0; i<sysFuns.length; i++) {
    		var sfId = sysFuns[i].sfId
    		for(var j=0; j<curSfIds.length; j++) {
    			if(sfId == curSfIds[j]) {
    				count++;
    			}
    		}
    		
    	}
    	
    	var children = item.children || [];
    	for(var i=0; i<children.length; i++) {
    		var child = children[i];
    		count +=getOperateCount(child);
    	}
    	
    	return count;
	}
	
	function addRolePermission(sfId) {
		setRolePermission({
			url:addRolePermissionUrl
			,sfId:sfId
			,roleId:curRoleId
		});
	}
	
	function delRolePermission(sfId) {
		setRolePermission({
			url:delRolePermissionUrl
			,sfId:sfId
			,roleId:curRoleId
		});
	}
	
	function setRolePermission(param) {
		var url = param.url;
		
		var data = {
			sfId:param.sfId
			,roleId:param.roleId
		}
		
		Action.post(url,data,function(e){
			Action.execResult(e);
		});
	}
	
	function reset() {
		if(grid) {
			grid.removeData();
		}
		if(tab) {
			tab.select(0);
		}
	}
	
	function setValue(roleId,sfIds) {
		curRoleId = roleId;
		curSfIds = sfIds;
	}
	
	function show(roleId,callback) {
		init(roleId,callback);
		reset();
		win.show();
	}
	
	return {
		show:function(roleId,callback) {
			
			var that = this;
			VUI.use(['Tab','Tree','Grid','Dialog'],function(){
				show(roleId,callback);
				
				that.show = function(roleId,callback) {
					show(roleId,callback);
				}
				
			});
		}
		,hide:function() {
			win.hide();
		}
	};
	
	
})();
